package vip.mengqin.diandezhun.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.util.Util;
import com.android.baselibrary.view.Comm_EditView;
import com.android.baselibrary.view.GridSpacingItemDecoration;
import com.android.commcount.request.Recharge_Price_Item_Request;
import com.android.commcount.responce.RechargePriceData;

import java.util.ArrayList;
import java.util.List;

import vip.mengqin.diandezhun.R;
import vip.mengqin.diandezhun.request.RechargeRequest;
import vip.mengqin.diandezhun.responce.RechargeData;

/**
 * Created by Administrator on 2018/8/22.
 */

public class RechargeCenter_ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    CommCallBack callBack;
    List<RechargeData> rechargeDataList;

    List<RechargeRequest> list = new ArrayList<>();
    public RechargePriceData rechargePriceData;

    ContentViewHolder currentViewHolder;

    public RechargeCenter_ListAdapter(Context mContext, CommCallBack callBack) {
        this.mContext = mContext;
        this.callBack = callBack;
    }

    //面值模板
    public void setRechargeDataList(List<RechargeData> rechargeDataList) {
        this.rechargeDataList = rechargeDataList;
    }

    //折扣信息
    public void setRechargePriceData(RechargePriceData rechargePriceData) {
        this.rechargePriceData = rechargePriceData;
        notifyDataSetChanged();
    }

    //添加用户
    public void addUser() {
        RechargeRequest rechargeRequest = new RechargeRequest();
        rechargeRequest.rechargeDataList.addAll(rechargeDataList);

        list.add(rechargeRequest);
        notifyDataSetChanged();
    }

    //手机号赋值
    public void setContact(String number) {
        if (currentViewHolder != null) {
            currentViewHolder.et_phone.setText(number);
        }
    }

    //获取列表
    public List<Recharge_Price_Item_Request> getRequestList() {
        List<Recharge_Price_Item_Request> requestList = new ArrayList<>();
        for (RechargeRequest rechargeRequest : list) {
            Recharge_Price_Item_Request price_item_request = new Recharge_Price_Item_Request();
            price_item_request.mobile = rechargeRequest.mobile;
            price_item_request.rechargeId = rechargeRequest.rechargeId;
            requestList.add(price_item_request);
        }
        return requestList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        ViewGroup viewGroup = (ViewGroup) mInflater.inflate(R.layout.item_recharge_center, null, false);
        ContentViewHolder holder = new ContentViewHolder(viewGroup);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ContentViewHolder viewHolder = (ContentViewHolder) holder;
        final RechargeRequest info = list.get(position);
        //数据绑定逻辑
        viewHolder.adapter.setData(info.rechargeDataList);

        if (position == list.size() - 1 && rechargePriceData != null) {
            viewHolder.tv_tpis.setVisibility(View.VISIBLE);
            String text = "";
            if (!rechargePriceData.isHigher) {
                text = "现在折扣为 " + rechargePriceData.currentDiscount + " 折，再增加 1 人，所有人的充值打 " + rechargePriceData.nextDiscount + " 折，现充值金额为  " + rechargePriceData.allPrice + "  元，打折后金额为  " + rechargePriceData.price + "  元。";
            } else {
                text = "现在为最高 " + rechargePriceData.currentDiscount + " 折，再增加的所有人，都能享受此优惠，充值打 " + rechargePriceData.nextDiscount + " 折，现充值金额为 " + rechargePriceData.allPrice + " 元，打折后金额为 " + rechargePriceData.price + " 元。";
            }
            viewHolder.tv_tpis.setText(text);
        } else {
            viewHolder.tv_tpis.setVisibility(View.GONE);
        }

        viewHolder.adapter.callBack = new CommCallBack() {
            @Override
            public void onResult(Object obj) {
                info.rechargeId = info.rechargeDataList.get(viewHolder.adapter.select).id;
                if (callBack != null) {
                    callBack.onResult("change");
                }
            }
        };

        viewHolder.et_phone.setInputCallBack(new CommCallBack() {
            @Override
            public void onResult(Object obj) {
                if (viewHolder.et_phone.getText().length() == 11) {
                    //输入成功
                    info.mobile = viewHolder.et_phone.getText();
                    if (callBack != null) {
                        callBack.onResult("change");
                    }
                }
            }
        });

        //删除
        viewHolder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(info);
                notifyDataSetChanged();
                if (callBack != null) {
                    callBack.onResult("change");
                }
            }
        });
        //通讯录
        viewHolder.tv_to_contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    currentViewHolder = viewHolder;
                    callBack.onResult("toContact");
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ContentViewHolder extends RecyclerView.ViewHolder {
        RecyclerView recyclerview;
        RechargeCenter_ValueListAdapter adapter;
        TextView tv_delete, tv_tpis, tv_to_contact;
        Comm_EditView et_phone;

        CommCallBack subItem_callBack = new CommCallBack() {
            @Override
            public void onResult(Object obj) {
                if (callBack != null) {
                    if (callBack != null) {
                        callBack.onResult("change");
                    }
                }
            }
        };

        public ContentViewHolder(View itemView) {
            super(itemView);

            adapter = new RechargeCenter_ValueListAdapter(mContext, subItem_callBack);
            recyclerview = itemView.findViewById(R.id.recyclerview);
            recyclerview.setLayoutManager(new GridLayoutManager(mContext, 2));
            recyclerview.addItemDecoration(new GridSpacingItemDecoration(2, Util.dip2px(mContext, 10), false));
            recyclerview.setAdapter(adapter);

            tv_delete = itemView.findViewById(R.id.tv_delete);
            tv_tpis = itemView.findViewById(R.id.tv_tpis);
            et_phone = itemView.findViewById(R.id.et_phone);
            tv_to_contact = itemView.findViewById(R.id.tv_to_contact);
        }
    }
}
